package com.easyskool.services.core;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONException;

import com.easyskool.composer.ModeratorComposer;
import com.easyskool.constants.CommonConstants;
import com.easyskool.utilfunction.UtilFunctions;
import com.sun.jersey.spi.container.servlet.PerSession;

@Path("/moderatorservice")
@PerSession
public class ModeratorService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ModeratorComposer composer = new ModeratorComposer();

	@Path("/insertModeratorDetails")
	@POST
	@Produces("application/json")
	public Response insertModeratorDetails(@QueryParam("user") String user,
			@QueryParam("fields") String insertValues,
			@Context HttpServletRequest request) throws Exception {
		String result = null;
		// HttpSession session = request.getSession();
		// user=(String) session.getAttribute("username");
		System.out.println("usegrgd : fvgfg" + user);
		result = composer.createModeratorDetails(user, insertValues);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/getModeratorDetails")
	@POST
	@Produces("application/json")
	public Response getModeratorDetails(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.getModeratorDetails(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/updateModeratorDetails")
	@POST
	@Produces("application/json")
	public Response updateModeratorDetails(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@QueryParam("updateClause") String updateClause) throws Exception {
		String result = null;
		result = composer.updateModeratorDetails(user, whereClause,
				updateClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/deleteModeratorsDetail")
	@POST
	@Produces("application/json")
	public Response deleteModeratorDetails(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.deleteModeratorDetails(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/deleteSelectedModeratorDetails")
	@POST
	@Produces("application/json")
	public Response deleteSelectedMasterClass(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.deleteSelectedModeratorDetails(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}
	@Path("/getClassesAndSections")
	@POST
	@Produces("application/json")
	public Response getClassesAndSections(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@Context HttpServletRequest request) throws JSONException  {
		String result = null;
		
		HttpSession session= request.getSession(true);
		  int schoolCode=(int) session.getAttribute(CommonConstants.SCHOOL_CODE);
		  System.out.println("SchoolCode in getClassesAndSections service: "+schoolCode);
		try{
		result = composer.getClassesAndSections(schoolCode);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return  UtilFunctions.createSuccessResponse(200,
					CommonConstants.OPERATION, CommonConstants.ERROR) ;
		}
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}
	@Path("/getSchoolTeachers")
	@POST
	@Produces("application/json")
	public Response getSchoolTeachers(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@Context HttpServletRequest request) throws JSONException  {
		String result = null;
		
		HttpSession session= request.getSession(true);
		  int schoolCode=(int) session.getAttribute(CommonConstants.SCHOOL_CODE);
		  System.out.println("SchoolCode in getSchoolTeachers service: "+schoolCode);
		try{
		result = composer.getSchoolTeachers(schoolCode);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return  UtilFunctions.createSuccessResponse(200,
					CommonConstants.OPERATION, CommonConstants.ERROR) ;
		}
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}
	@Path("/getSubjectsForSchool")
	@POST
	@Produces("application/json")
	public Response getSubjectsForSchool(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@Context HttpServletRequest request) throws JSONException, JsonParseException, JsonMappingException, IOException  {
		String result = null;
		
		HttpSession session= request.getSession(true);
		  int schoolCode=(int) session.getAttribute(CommonConstants.SCHOOL_CODE);
		  System.out.println("SchoolCode in getSubjectsForClass service: "+whereClause);
		try{
		result = composer.getSubjectsForSchool(schoolCode,whereClause);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return  UtilFunctions.createSuccessResponse(200,
					CommonConstants.OPERATION, CommonConstants.ERROR) ;
		}
		System.out.println("result in Moderator Service getSubjectsForSchool  : "  + result);
		return Response.status(200).entity(result).build();
	}

 
}
